//
//  LYQLoginDefaultCell.m
//  ZCProject
//
//  Created by Zheng on 16/11/15.
//  Copyright © 2016年 Zheng. All rights reserved.
//

#import "LYQLoginDefaultCell.h"

#import "LYQLoginModel.h"

@implementation LYQLoginDefaultCell

- (instancetype)init {
    self = [super init];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    return self;
}

- (void)setModel:(LYQLoginModel *)model {
    
    _model = model;
    
//    if (_nationNameLbl) {
//        _model.areaStr = _nationNameLbl.text;
//    }
    
}

#pragma mark - 设置类型
- (void)setCellStyle:(LYQLoginDefaultCellStyle)cellStyle {
    
    if (cellStyle == LYQLoginDefaultCellStyleNation) {
        
        UIImage *aImage = [UIImage imageNamed:@"morejticon"];
        UIImageView *aImgv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, aImage.size.width, aImage.size.height)];
        aImgv.image = aImage;
        self.accessoryView = aImgv;
        
        [self.contentView addSubview:self.nationTitleLbl];
        [self.contentView addSubview:self.nationNameLbl];
        
        [_nationTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(kWScale(15));
            make.centerY.equalTo(self);
        }];
        
        [_nationNameLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(kWScale(120));
            make.centerY.equalTo(_nationTitleLbl);
        }];
    } else if (cellStyle == LYQLoginDefaultCellStyleUsername) {
        
        [self.contentView addSubview:self.areaCodeLbl];
        [self.contentView addSubview:self.lineView];
        [self.contentView addSubview:self.phoneTfd];
        
        [_areaCodeLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(kWScale(15));
            make.centerY.equalTo(self);
        }];
        
        [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(kWScale(85));
            make.top.mas_equalTo(kWScale(5));
            make.bottom.mas_equalTo(-kWScale(5));
            make.width.equalTo(@(kWScale(0.5)));
        }];
        
        [_phoneTfd mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(kWScale(120));
            make.right.mas_equalTo(-kWScale(15));
            make.top.bottom.equalTo(self);
        }];
        
    } else if (cellStyle == LYQLoginDefaultCellStylePassword) {
        
        [self.contentView addSubview:self.passwordTfd];
        
        [_passwordTfd mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(kWScale(15));
            make.right.mas_equalTo(-kWScale(15));
            make.top.bottom.equalTo(self);
        }];
        
    } else if (cellStyle == LYQLoginDefaultCellStyleVerification) {
        
        [self.contentView addSubview:self.verificationTfd];
        [self.contentView addSubview:self.vLineView];
        [self.contentView addSubview:self.getVerifticationBtn];
        
        [_verificationTfd mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(kWScale(15));
            make.right.mas_equalTo(-kWScale(85));
            make.top.bottom.equalTo(self);
        }];
        
        [_vLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_verificationTfd.mas_right);
            make.top.mas_equalTo(kWScale(5));
            make.bottom.mas_equalTo(-kWScale(5));
            make.width.equalTo(@(kWScale(0.5)));
        }];
        
        [_getVerifticationBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_vLineView.mas_right);
            make.right.top.bottom.equalTo(self);
        }];
        
    } else if (cellStyle == LYQLoginDefaultCellStylePassword2) {
        
        [self.contentView addSubview:self.password2Tfd];
        [self.contentView addSubview:self.pLineView];
        [self.contentView addSubview:self.passwordStatusBtn];
        
        [_password2Tfd mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(kWScale(15));
            make.right.mas_equalTo(-kWScale(85));
            make.top.bottom.equalTo(self);
        }];
        
        [_pLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_password2Tfd.mas_right);
            make.top.mas_equalTo(kWScale(5));
            make.bottom.mas_equalTo(-kWScale(5));
            make.width.equalTo(@(kWScale(0.5)));
        }];
        
        [_passwordStatusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_pLineView.mas_right);
            make.right.top.bottom.equalTo(self);
        }];
        
    }
    
}

#pragma mark - load subview
#pragma mark nation cell style
- (UILabel *)nationTitleLbl {
    if (!_nationTitleLbl) {
        _nationTitleLbl = [[UILabel alloc] init];
        _nationTitleLbl.text = @"国家/区域";
        _nationTitleLbl.font = kWFont(14);
        _nationTitleLbl.textColor = kHexadecimal(@"666666");
    }
    return _nationTitleLbl;
}

- (UILabel *)nationNameLbl {
    if (!_nationNameLbl) {
        _nationNameLbl = [[UILabel alloc] init];
        _nationNameLbl.text = @"中国";
        _nationNameLbl.font = kWFont(15);
        _nationNameLbl.textColor = kHexadecimal(@"666666");
    }
    return _nationNameLbl;
}

#pragma mark username cell style
- (UILabel *)areaCodeLbl {
    if (!_areaCodeLbl) {
        _areaCodeLbl = [[UILabel alloc] init];
        _areaCodeLbl.textColor = kHexadecimal(@"666666");
        _areaCodeLbl.font = kWFont(14);
        _areaCodeLbl.text = @"+ 86";
    }
    return _areaCodeLbl;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = kLYQSystemGray;
    }
    return _lineView;
}

- (UITextField *)phoneTfd {
    if (!_phoneTfd) {
        _phoneTfd = [[UITextField alloc] init];
        _phoneTfd.placeholder = @"输入手机号";
        _phoneTfd.font = kWFont(15);
        _phoneTfd.clearButtonMode = UITextFieldViewModeWhileEditing;
        [_phoneTfd addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];
        _phoneTfd.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _phoneTfd;
}

#pragma mark password cell style
- (UITextField *)passwordTfd {
    if (!_passwordTfd) {
        _passwordTfd = [[UITextField alloc] init];
        _passwordTfd.placeholder = @"输入6位以上密码";
        _passwordTfd.font = kWFont(15);
        _passwordTfd.secureTextEntry = YES;
        _passwordTfd.clearButtonMode = UITextFieldViewModeWhileEditing;
        [_passwordTfd addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];

    }
    return _passwordTfd;
}

#pragma mark verification cell style
- (UITextField *)verificationTfd {
    if (!_verificationTfd) {
        _verificationTfd = [[UITextField alloc] init];
        _verificationTfd.placeholder = @"输入验证码";
        _verificationTfd.font = kWFont(15);
        _verificationTfd.clearButtonMode = UITextFieldViewModeWhileEditing;
        [_verificationTfd addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];
        _verificationTfd.keyboardType = UIKeyboardTypeNumberPad;

    }
    return _verificationTfd;
}

- (UIView *)vLineView {
    if (!_vLineView) {
        _vLineView = [[UIView alloc] init];
        _vLineView.backgroundColor = kLYQSystemGray;
    }
    return _vLineView;
}

- (UIButton *)getVerifticationBtn {
    if (!_getVerifticationBtn) {
        _getVerifticationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_getVerifticationBtn setTitleColor:RGB(251, 116, 81, 1) forState:UIControlStateNormal];
        [_getVerifticationBtn setTitle:@"获取" forState:UIControlStateNormal];
        _getVerifticationBtn.titleLabel.font = kWFont(15);
        [_getVerifticationBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        _getVerifticationBtn.tag = 34500;
    }
    return _getVerifticationBtn;
}

#pragma mark - password2 cell style
- (UITextField *)password2Tfd {
    if (!_password2Tfd) {
        _password2Tfd = [[UITextField alloc] init];
        _password2Tfd.placeholder = @"输入6位以上密码";
        _password2Tfd.font = kWFont(15);
        _password2Tfd.secureTextEntry = YES;
        _password2Tfd.clearButtonMode = UITextFieldViewModeWhileEditing;
        [_password2Tfd addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _password2Tfd;
}

- (UIView *)pLineView {
    if (!_pLineView) {
        _pLineView = [[UIView alloc] init];
        _pLineView.backgroundColor = kLYQSystemGray;
    }
    return _pLineView;
}

- (UIButton *)passwordStatusBtn {
    if (!_passwordStatusBtn) {
        _passwordStatusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_passwordStatusBtn setTitleColor:RGB(251, 116, 81, 1) forState:UIControlStateNormal];
        [_passwordStatusBtn setTitle:@"显示" forState:UIControlStateNormal];
        [_passwordStatusBtn setTitle:@"隐藏" forState:UIControlStateSelected];
        _passwordStatusBtn.titleLabel.font = kWFont(15);
        [_passwordStatusBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        _passwordStatusBtn.tag = 34501;
    }
    return _passwordStatusBtn;
}


#pragma mark - 编辑事件
- (void)textDidChange:(UITextField *)textField {
    
    NSInteger length = 0;
    
    if (textField == _phoneTfd) {
        _model.phoneStr = _phoneTfd.text;
        length = 11;
    } else if (textField == _passwordTfd) {
        _model.passwordStr = _passwordTfd.text;
        length = 12;
    } else if (textField == _verificationTfd) {
        _model.verifticationStr = _verificationTfd.text;
        length = 6;
    } else if (textField == _password2Tfd) {
        _model.passwordStr = _password2Tfd.text;
        length = 12;
    }
    
    if (textField.text.length > length) {
        textField.text = [textField.text substringToIndex:length];
        if (length == 11) {
            _model.phoneStr = textField.text;
        }
        
        if (length == 6) {
            _model.verifticationStr = textField.text;
        }
        
        if (length == 12) {
            _model.passwordStr = textField.text;
        }
    }
    
}

#pragma mark - 点击事件
- (void)buttonAction:(UIButton *)sender {
    
    if (sender.tag == 34500) {
#pragma mark 发送短信
        _getVerifticationBtn.userInteractionEnabled = NO;

        if (_model.phoneStr) {
            
            NSDictionary *param = @{@"mobile":_model.phoneStr};
            
            [NetRequest GetWithRequestStyle:NetRequestHostTypeStyleDefault withAPI:@"index.php/member/member/sendsms.html" param:param success:^(id response) {
                
                if ([response[@"code"] isEqualToNumber:@1]) {
                    [self getVerificationCountdown];
                }
                
                _getVerifticationBtn.userInteractionEnabled = YES;

            } error:^(NSError *error) {
                _getVerifticationBtn.userInteractionEnabled = YES;

            }];
            
        } else {
            _getVerifticationBtn.userInteractionEnabled = YES;
            [[TKAlertCenter defaultCenter] postAlertWithMessage:@"手机号码不能为空"];
        }
        
    } else if (sender.tag == 34501) {
#pragma mark 显示或隐藏密码
        _passwordStatusBtn.selected = !_passwordStatusBtn.selected;
        _password2Tfd.secureTextEntry = !_password2Tfd.secureTextEntry;
    }
    
}

#pragma mark - 验证码倒计时
- (void)getVerificationCountdown {
    __block int timeout = 60;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(timer,dispatch_walltime(NULL, 0), 1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(timer, ^{
        if(timeout <= 0){
            dispatch_source_cancel(timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [_getVerifticationBtn setTitle:@"获取" forState:UIControlStateNormal];
                _getVerifticationBtn.userInteractionEnabled = YES;
            });
        } else {
            NSString *timeStr = [NSString stringWithFormat:@"%d s", timeout];
            dispatch_async(dispatch_get_main_queue(), ^{
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                [_getVerifticationBtn setTitle:timeStr forState:UIControlStateNormal];
                [UIView commitAnimations];
                _getVerifticationBtn.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(timer);

}


@end
